<template>
    <view class="container">
        <header-bar :config="config"></header-bar>
        <view class="main">
            <view class="card-cell type-cell">
                <view class="cell-title flex-s-c" style="width: 100%">
                    <view>备注:</view>
                </view>
                <textarea
                    class="textarea"
                    :maxlength="maxLengthText"
                    @input="sum"
                    v-model="content"
                    placeholder="请输入备注内容"></textarea>
                <view class="number"
                    ><text class="pr10"
                        ><text class="strong">{{ num }}</text
                        >/{{ maxLengthText }}
                    </text></view
                >
            </view>
            <view class="card-cell type-cell">
                <text class="cell-title">上传图片</text>
                <view class="img-cell">
                    <view class="" v-if="imageList.length < 9">
                        <view class="add-img" @tap="chooseImage">
                            <uni-icons type="camera" size="80"></uni-icons>
                            <text>添加图片</text>
                        </view>
                    </view>
                    <block v-for="(image, index) in imageList" :key="index">
                        <view class="img-item">
                            <image class="img" :src="image" :data-src="image" @tap="previewImage"></image>
                            <view @click="del(index)">
                                <uni-icons class="btn-del" type="closeempty" customIcons="icon-white"></uni-icons>
                            </view>
                        </view>
                    </block>
                </view>
            </view>

            <view class="acton-bar flex-s-c">
                <button class="btn btn-success" @click="takeOrderFn">确认提交</button>
            </view>
        </view>
    </view>
</template>

<script>
import { mapMutations } from "vuex"
import { uploadImage } from "@/service/api/file.js"
import { TakeOrder } from "@/service/api/order.js"
export default {
    data() {
        return {
            config: {
                title: "确认收货",
                centerSlot: true,
            },
            num: 0,
            imageList: [],
            content: "",
            maxLengthText: "100",
        }
    },
    onLoad(e) {
        this.orderId = e.orderId
    },
    methods: {
        ...mapMutations(["UP_ORDER_VIEW"]),
        sum(e) {
            this.num = e.detail.value.length
        },
        //预览图片
        previewImage(e) {
            var current = e.target.dataset.src
            uni.previewImage({
                current: current,
                urls: this.imageList,
            })
        },
        //选择图片
        chooseImage() {
            let that = this
            uni.chooseImage({
                count: 9, //默认9
                sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
                sourceType: ["album", "camera"], //从相册选择
                success: function (res) {
                    that.upLoadPicFn(res)
                },
            })
        },
        //删除图片
        del(index) {
            this.imageList.splice(index, 1)
        },
        //上传图片
        upLoadPicFn(files) {
            for (var i = 0; i < files.tempFilePaths.length; i++) {
                let param = {
                    filePath: files.tempFilePaths[i],
                    formData: {},
                }
                uploadImage(param).then((e) => {
                    if (e.Data) {
                        this.imageList.push(e.Data.FullPath)
                        console.log(this.imageList.length)
                        if (this.imageList.length > 9) {
                            this.imageList.length = 9
                        }
                    }
                })
            }
        },
        takeOrderFn() {
            if (this.imageList.length < 1) {
                this.easyToast("请上传图片")
                return
            }
            let parms = {
                orderId: this.orderId,
                remark: this.content,
                images: this.imageList.join(","),
            }

            TakeOrder(parms).then((res) => {
                if (res.State == 1) {
                    this.easyToast("操作成功")
                    this.upOrderView()
                    setTimeout((e) => {
                        this.easyGoHome()
                    }, 800)
                } else {
                    this.easyToast(res.Msg)
                }
            })
        },
        upOrderView() {
            this.UP_ORDER_VIEW(new Date().getTime())
        },
    },
}
</script>

<style lang="scss">
.main {
    padding: $app-page-padding;
}

.type-cell {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    background: #ffffff;
    border-radius: 8rpx;
    padding: 34rpx 24rpx 10rpx 24rpx;
    margin-bottom: 24rpx;
    .cell-title {
        display: flex;
        flex: 1;
        font-size: 30rpx;
        color: $color-gray-1;
        margin-bottom: 32rpx;
    }

    .types {
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: space-between;

        .btn {
            width: 140rpx;
            padding: 0;
            border-radius: 10rpx;
            font-size: 26rpx;
            height: 52rpx;
            border: 1rpx solid #f6f6f6;
            color: $color-gray-1;
            margin-bottom: 20rpx;
            background: #f6f6f6;
        }

        .btnactive {
            background: #ffffff;
            color: #ffae45;
            border-color: #ffae45;
        }

        &:after {
            content: " ";
            width: 48%; //这个宽度和子元素宽度一致
            height: 0;
            display: block;
        }
    }
}

.textarea {
    display: flex;
    width: 98%;
    padding: 1%;
    min-height: 140rpx;
    font-size: $font-size-24;
    line-height: 40rpx;
    height: 236rpx;
    background: #f6f7f7;
    border-radius: 8rpx;
    // border: 1rpx solid #ec808d;
}
.acton-bar {
    margin-top: 70rpx;

    .btn {
        width: 100%;
        height: 80rpx;
    }
}
.number {
    padding-top: 10rpx;
    width: 100%;
    display: flex;
    justify-content: flex-end;
    font-size: 24rpx;

    background: #f6f7f7;
    padding-bottom: 10rpx;
    margin-bottom: 10rpx;
    .pr10 {
        padding-right: 10rpx;
        color: #cccccc;
    }
    .strong {
        font-size: 26rpx;
    }
}
.img-cell {
    display: flex;
    flex: 1;
    flex-direction: row;
    flex-wrap: wrap;
    margin-bottom: 20rpx;

    &:after {
        content: " ";
        width: 33%; //这个宽度和子元素宽度一致
        height: 0;
        display: block;
    }

    .img-item {
        position: relative;
        width: 180rpx;
        height: 180rpx;
        margin-bottom: $app-item-padding;
        margin-right: $app-item-padding;

        .img {
            width: 180rpx;
            height: 180rpx;
            border-radius: $border-radius-10;
        }

        .btn-del {
            display: flex;
            justify-content: center;
            align-items: center;
            position: absolute;
            top: -36rpx;
            right: -36rpx;
            width: 50rpx;
            height: 50rpx;
            border-radius: 40rpx;
            background: rgba($color-black, 0.3);
            color: $color-white;
            margin: 15rpx;
        }
    }

    .add-img {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        width: 178rpx;
        height: 178rpx;
        border-radius: $border-radius-10;
        border: dashed 1rpx $color-gray-6;
        margin-right: $app-item-padding;
        margin-bottom: $app-item-padding;

        text {
            margin-top: 6rpx;
        }
    }
}
.tip {
    color: #f0201f;
    font-size: 24rpx;
    display: flex;
    align-items: center;
    .btn-location {
        border: none;
    }
}
</style>
